<template>
  <div class="app-container">
    <el-row style="margin:0 0 5px 5px" v-show="showSearch">
      <!-- 搜索区域 -->
      <el-card style="margin:0 0 5px 5px;">
        <el-form ref="queryForm" :inline="true" :model="queryData">
          <el-form-item label="订单号" prop="Q_orderSn_S_LK">
            <el-input v-model="queryData.Q_orderSn_S_LK" placeholder="订单号"/>
          </el-form-item>
          <el-form-item label="交易流水号" prop="Q_refundSn_S_LK">
            <el-input v-model="queryData.Q_refundSn_S_LK" placeholder="交易流水号"/>
          </el-form-item>
          <el-form-item class="fr p-r-10">
            <el-button class="filter-item" type="primary" round icon="el-icon-search" @click="handleFilter">
              搜索
            </el-button>
            <el-button class="filter-item m-l-10" type="info" round icon="el-icon-refresh" @click="handleClear">
              清空
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <!-- table区域 -->
      <el-card class="m-l-5">
        <el-row :gutter="10" class="m-b-5">
          <table-toolbar :showSearch.sync="showSearch" @refresh="getTableData"/>
        </el-row>
        <el-table
          ref="queryTable"
          v-loading="tableLoading"
          row-key="id"
          :data="tableData"
          element-loading-text="加载中"
          border
          fit
          highlight-current-row
          show-overflow-tooltip
          height="100px"
          v-el-height-adaptive-table="{bottomOffset: 110}"
          @sort-change="sortChange"
        >
          <el-table-column align="center" label="订单号" prop="orderSn" width="200" show-overflow-tooltip />
          <el-table-column align="center" label="交易流水号" prop="refundSn" width="200" show-overflow-tooltip />
          <el-table-column align="center" label="交易渠道" prop="refundChannel" width="150">
            <template slot-scope="scope">
              <el-link :type="getDictColor('channel', scope.row.refundChannel)" :underline="false">
                {{ getDictValue('channel', scope.row.refundChannel) }}
              </el-link>
            </template>
          </el-table-column>
          <el-table-column align="center" label="退款金额" prop="refundAmount" width="150" />
          <el-table-column align="center" label="退款状态" prop="refundStatus" sortable width="120">
            <template slot-scope="scope">
              <el-link :type="getDictColor('status', scope.row.refundStatus)" :underline="false">
                {{ getDictValue('status', scope.row.refundStatus) }}
              </el-link>
            </template>
          </el-table-column>
          <el-table-column align="center" label="内容" prop="refundContent" />
          <el-table-column align="center" label="操作" width="90" fixed="right">
            <template slot-scope="scope">
              <el-link type="danger" icon="el-icon-delete" @click="handleDelete(scope.row.id)">删除</el-link>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="tableQuery.total>0" :total="tableQuery.total" :page.sync="tableQuery.page" :limit.sync="tableQuery.limit" @pagination="getTableData"/>
      </el-card>
    </el-row>
  </div>
</template>

<script>
import { refundInfoTable, refundInfoDelete } from '@/api/mall/payment/pay'

export default {
  data() {
    return {
      showSearch: true,
      queryData: {
        Q_orderSn_S_LK: undefined,
        Q_refundSn_S_LK: undefined
      },
      optionOfChannel: [],
      optionOfStatus: [],
      tableLoading: false,
      tableData: [],
      tableQuery: {
        page: 1,
        limit: 20,
        total: 0,
        sort: undefined,
        order: undefined
      }
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.$nextTick(() => {
        this.getDictData()
      })
    },
    getDictData() {
      const that = this
      this.$http.all([this.$dictGroup('sys_pay_type'), this.$dictGroup('sys_available_type')])
        .then(this.$http.spread(function(response1, response2) {
          that.optionOfChannel = response1
          that.optionOfStatus = response2
          that.getTableData()
        }))
    },
    getDictValue(property, value) {
      return this.getDictElement(property, value, 1)
    },
    getDictColor(property, value) {
      return this.getDictElement(property, value, 2)
    },
    getDictElement(property, value, index) {
      if (property === 'channel') {
        return this.$api.getDictElement(this.optionOfChannel, value)[index]
      } else if (property === 'status') {
        return this.$api.getDictElement(this.optionOfStatus, value)[index]
      }
    },
    handleFilter() {
      this.tableQuery.page = 1
      this.getTableData()
    },
    handleClear() {
      this.$refs.queryForm.resetFields()
      this.getTableData()
    },
    getTableData() {
      const query = Object.assign(this.tableQuery, this.queryData)
      this.tableLoading = true
      refundInfoTable(query).then(response => {
        this.tableData = response.param.data
        this.tableQuery.total = response.param.recordsTotal
        this.tableLoading = false
      }).catch(e => {
        this.tableLoading = false
      })
    },
    sortChange(data) {
      const { prop, order } = data
      this.tableQuery.sort = prop
      if (order === 'ascending') {
        this.tableQuery.order = 'asc'
      } else {
        this.tableQuery.order = 'desc'
      }
      this.getTableData()
    },
    handleDelete(id) {
      this.$confirm('是否删除所选数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'danger'
      }).then(() => {
        refundInfoDelete(id).then(response => {
          this.handleFilter()
          this.$modal.msgSuccess(response.msg)
        })
      })
    }
  }
}
</script>
